package com.lagou.rpc.consumer.balance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhangGJ
 * @Date 2021/06/30 06:56
 */
public class LoadBalanceImpl implements LoadBalance {

    private int index;

    private List<Map<String, Object>> serverList = new ArrayList<>();

    public LoadBalanceImpl() {
        Map<String, Object> map = new HashMap<>();
        map.put("server", "127.0.0.1");
        map.put("port", 8899);
        serverList.add(map);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("server", "127.0.0.1");
        map1.put("port", 8898);
        serverList.add(map1);
    }

    @Override
    public synchronized Map<String, Object> getServer() {
        if (index >= serverList.size()) {
            index = 0;
        }
        Map<String, Object> server = serverList.get(index++);
        System.out.println("=========================Current server: " + server);
        return server;
    }
}
